using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Ares.Protocols;
using Ares.CommonObjects;

namespace cb0tServer
{
    partial class ServerCore
    {
        private bool WhisperFilter(String text)
        {
            String str = Helpers.StripColors(text);
            FilterItem[] items = WordFilters.GetFilters();

            if (items == null) return true;

            foreach (FilterItem filter in items)
            {
                if (str.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                {
                    switch (filter.type)
                    {
                        case "replace":
                        case "muzzle":
                        case "kiddy":
                        case "kill":
                        case "ban":
                        case "censor":
                        case "move":
                        case "redirect":
                        case "pm":
                            return false;
                    }
                }
            }

            return true;
        }

        private String OnWordFilterBefore(UserObject userobj, String text)
        {
            String str = Helpers.StripColors(text);
            FilterItem[] items = WordFilters.GetFilters();

            if (items == null) return text;

            foreach (FilterItem filter in items)
            {
                if (userobj.logged_in) // still connected?
                {
                    if (str.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                    {
                        switch (filter.type)
                        {
                            case "replace":
                                if (userobj.admin || userobj.host) break;
                                if (Regex.IsMatch(text, filter.trigger, RegexOptions.IgnoreCase))
                                    text = Regex.Replace(text, filter.trigger, filter.args, RegexOptions.IgnoreCase);
                                break;

                            case "muzzle":
                                if (userobj.admin || userobj.host || userobj.muzzled) break;
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(32).Replace("+n", userobj.name)));
                                userobj.muzzled = true;
                                Stats.MuzzledUsers++;
                                Muzzles.AddMuzzle(userobj);
                                AdminLog.WriteLine(userobj.name + " was muzzled for typing " + filter.trigger);
                                return "";

                            case "kiddy":
                                if (userobj.admin || userobj.host || userobj.kiddied) break;
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(33).Replace("+n", userobj.name)));
                                userobj.kiddied = true;
                                Kiddys.AddKiddy(userobj);
                                AdminLog.WriteLine(userobj.name + " was kiddied for typing " + filter.trigger);
                                return "";

                            case "kill":
                                if (userobj.admin || userobj.host) break;
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(34).Replace("+n", userobj.name)));
                                userobj.Disconnect();
                                this.OnPart(userobj);
                                Stats.UsersParted++;
                                Stats.KickedUsers++;
                                AdminLog.WriteLine(userobj.name + " was kicked for typing " + filter.trigger);
                                return "";

                            case "ban":
                                if (userobj.admin || userobj.host) break;
                                UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(35).Replace("+n", userobj.name)));
                                this.banstats.Add(userobj.name + " [" + userobj.physical_ip.ToString() + "] was banned by the word filter");
                                Bans.AddBan(userobj);
                                userobj.Disconnect();
                                this.OnPart(userobj);
                                Stats.UsersParted++;
                                Stats.BannedUsers++;
                                Stats.KickedUsers++;
                                AdminLog.WriteLine(userobj.name + " was banned for typing " + filter.trigger);
                                return "";

                            case "censor":
                                if (userobj.admin || userobj.host) break;
                                userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(36).Replace("+n", userobj.name)));
                                return "";

                            case "move":
                                if (userobj.admin || userobj.host) break;
                                ushort wfmove;

                                if (ushort.TryParse(filter.args, out wfmove))
                                {
                                    if (userobj.vroom != wfmove)
                                    {
                                        if (this.OnVroomJoinCheck(userobj, wfmove))
                                        {
                                            this.OnVroomJoin(userobj, wfmove);
                                            UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(37).Replace("+n", userobj.name)));
                                            AdminLog.WriteLine(userobj.name + " was moved for typing " + filter.trigger);
                                        }
                                    }
                                }

                                return "";

                            case "redirect":
                                if (userobj.admin || userobj.host) break;

                                ChannelListItem clistitem = Hashlinks.DecodeHashlink(filter.args);

                                if (clistitem != null)
                                {
                                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(38).Replace("+n", userobj.name)));
                                    userobj.SendOutboundPacket(ServerOutboundPackets.RedirectPacket(clistitem));
                                    userobj.Disconnect();
                                    this.OnPart(userobj);
                                    Stats.UsersParted++;
                                    AdminLog.WriteLine(userobj.name + " was redirected for typing " + filter.trigger);
                                }

                                return "";

                            case "pm":
                                userobj.SendOutboundPacket(ServerOutboundPackets.PMPacket(CoreSettings.room_bot,
                                    filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                                break;

                            case "announce":
                                if (userobj.admin || userobj.host || !Settings.adminannounce)
                                {
                                    String[] ann_split = filter.args.Split(new String[] { "\0" }, StringSplitOptions.None);

                                    if (str.ToUpper().StartsWith(filter.trigger.ToUpper()) &&
                                        str.Length > filter.trigger.Length) // include +r ident
                                    {
                                        String r_ident = str.Substring(filter.trigger.Length + 1);

                                        foreach (String ann_line in ann_split)
                                        {
                                            UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.AnnoucePacket(ann_line.Replace("+n",
                                                userobj.name).Replace("+ip", userobj.physical_ip.ToString()).Replace("+r", r_ident)));
                                        }
                                    }
                                    else
                                    {
                                        foreach (String ann_line in ann_split)
                                        {
                                            UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.AnnoucePacket(ann_line.Replace("+n",
                                                userobj.name).Replace("+ip", userobj.physical_ip.ToString()).Replace("+r", "")));
                                        }
                                    }
                                }

                                break;

                            default:
                                break;
                        }
                    }
                }
                else
                {
                    return "";
                }
            }

            return text;
        }

        private void OnWordFilterAfter(UserObject userobj, String text)
        {
            if (userobj.admin || userobj.host) return;

            String str = Helpers.StripColors(text);
            FilterItem[] items = WordFilters.GetFilters();

            if (items == null) return;

            foreach (FilterItem filter in items)
            {
                if (filter.type == "clone")
                {
                    if (str.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                    {
                        UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.PublicTextPacket(userobj.name,
                            filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                        AdminLog.WriteLine(userobj.name + " was cloned for typing " + filter.trigger);
                    }
                }
            }
        }

    }
}
